package com.credit.filter;
import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.credit.context.CreditServletContext;
import com.credit.web.basic.bean.ResBean;



/**
 * 过滤用户请求
 * @author 
 *
 */
public class ServletContextFilter implements Filter {
	private static Logger log = Logger.getLogger(ServletContextFilter.class);
	
	public void destroy() {
		
		
	}

	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
        
        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse res = (HttpServletResponse) response;
        
        CreditServletContext.setHttpServlet(req, res);
        
        log.info("请求的接口为:[ "+req.getRequestURI()+" ]");
        
		Enumeration<String> enu = req.getHeaderNames();
		while(enu.hasMoreElements()){
			String key = enu.nextElement();
			String value = req.getHeader(key);
			log.info("请求的header参数 [ key = "+key+" ] , [ value = "+value+" ]");
		}
		
		Map paramsMap = req.getParameterMap();
		Map<String,String> scope = new HashMap<>();
		for(Object object:paramsMap.keySet()){
			String key = (String) object;
			String value = req.getParameter(key);
			log.info("请求的参数[ key = "+key+" ] , [ value = "+value+" ]");
			scope.put(key, value);
		}
		CreditServletContext.setScopeLocal(scope);
		ResBean resBean = new ResBean();
		CreditServletContext.setResBean(resBean);
		
		chain.doFilter(request, response);  
		
	}

	public void init(FilterConfig arg0) throws ServletException {
		
	}

}
